--
-- Author: shunguo.chen
-- Date: 2017-06-29 16:10:17
--
-- TabsScrollBtn 带滚动的，新版特殊的TabsBtn，定制版

local modeName = string.getModeName(...)
local ClassRef = class(modeName)

ClassRef._con = nil
ClassRef._callBack = nil
ClassRef._default = nil
ClassRef._binds = nil

ClassRef._leftBtn = nil
ClassRef._rightBtn = nil
ClassRef._tabs = nil
ClassRef._names = nil
ClassRef._dots = nil

ClassRef._pressedIdx = nil
ClassRef._pressedBtn = nil

--是否点击一选中的按钮依然响应事件
ClassRef._isAlwaysHander = nil

function ClassRef:ctor(con, callBack, default, binds)
	self._con = con
	self._callBack = callBack
	self._default = default or 1	
	self._binds = binds
	
	self._isAlwaysHander = false

    -- self._leftBtn = con:getChildByName("Button_arrow_1")
    -- self._rightBtn = con:getChildByName("Button_arrow_2")
    -- self._leftBtn:addClickEventListener(function(sender)
    -- 	local idx = self._pressedIdx - 1
    -- 	if idx <= 0 then idx = #self._tabs end
    -- 	self:pressBtn(idx, true)
    -- end)
    -- self._rightBtn:addClickEventListener(function(sender)
    -- 	local idx = self._pressedIdx + 1
    -- 	if idx > #self._tabs then idx = 1 end
    -- 	self:pressBtn(idx, true)
    -- end)

    self._tabs = {}
    self._names = {}
    -- self._dots = {}
    for i=1,100 do
    	local btn = con:getChildByName("Button_"..i)
    	if btn then
    		self._tabs[i] = btn
    		self._names[i] = con:getChildByName("name_"..i)
    		-- self._dots[i] = con:getChildByName("dot_"..i)
    	else
    		break
    	end
    end

	for index, btn in ipairs(self._tabs) do	
		self:setBtnSelected(index, false)
	    self:addEvent(btn, index)
	end

    self:pressBtn(self._default, true)
end

function ClassRef:addEvent(btn, index)
	btn:addClickEventListener(function()
		self:pressBtn(index, true)
	end)
end

function ClassRef:setAlwaysHander(b)
	self._isAlwaysHander = b
end

function ClassRef:setBtnSelected(index, b)
	self._tabs[index]:setBright(not b)
	self._names[index]:setOpacity(b and 255 or 115)
	-- self._dots[index]:setOpacity(b and 255 or 115)
end

function ClassRef:pressBtn(index, isCallBack)
	local preIdx = self._pressedIdx
	self._pressedIdx = index
	if preIdx ~= index then
	    if preIdx then
	    	self:setBtnSelected(preIdx, false)
	    end 
	    self:setBtnSelected(index, true)
	    self._pressedBtn = self._tabs[index]
	    	    
	    if nil ~= self._binds then
	        for key, panel in pairs(self._binds) do
	            panel:setVisible(key == index)
	        end
	    end
	    
	    if nil ~= self._callBack and isCallBack then
	        self._callBack(index, self._pressedBtn)
	    end
	else
		self:setBtnSelected(self._pressedIdx, true)
		if self._isAlwaysHander then
			if nil ~= self._callBack and isCallBack then
			    self._callBack(index, self._pressedBtn)
			end
		end
	end
end

function ClassRef:setTouchEnabled(enable)
	for index, tabBtn in ipairs(self._tabs) do
		tabBtn:setTouchEnabled(enable)
	end
end

function ClassRef:initSelectStatus()
	if nil ~= self._pressedIdx then
		self:setBtnSelected(self._pressedIdx, false)
	    self._pressedIdx = nil
	    self._pressedBtn = nil
	end
end

function ClassRef:setSelected(index, isCallBack)
	isCallBack = isCallBack or false
	self:pressBtn(index, isCallBack)
end

function ClassRef:getBtn(index)
	return self._tabs[index]
end

function ClassRef:getCurIdx()
	return self._pressedIdx
end

return ClassRef